/////////////////////////////////////////////////////////////////////////////
// Name:        choiceCustomer.cpp
// Purpose:     
// Author:      Herry Sugiharto
// Modified by: 
// Created:     23/07/2009 17:15:36
// RCS-ID:      
// Copyright:   Copyright (c) 2000, 2008 Krida Software Division
// Licence:     
/////////////////////////////////////////////////////////////////////////////

// For compilers that support precompilation, includes "wx/wx.h".
#include "wx/wxprec.h"

#ifdef __BORLANDC__
#pragma hdrstop
#endif

#ifndef WX_PRECOMP
#include "wx/wx.h"
#endif

#include "slposapp.h"
#include "choiceCustomer.h"


/*!
 * choiceCustomer type definition
 */

IMPLEMENT_DYNAMIC_CLASS( choiceCustomer, wxChoice )


/*!
 * choiceCustomer constructors
 */

choiceCustomer::choiceCustomer()
{
}


choiceCustomer::choiceCustomer(wxWindow* parent, 
                               wxWindowID id, 
                               const wxPoint& pos, 
                               const wxSize& size, 
                               const wxArrayString& choices, 
                               long style, 
                               const wxValidator& validator)
{
    Create(parent, id, pos, size, choices, style, validator);
}


/*!
 * choiceCustomer creator
 */

bool choiceCustomer::Create(wxWindow* parent, wxWindowID id, const wxPoint& pos, const wxSize& size, const wxArrayString& choices, long style, const wxValidator& validator)
{
    wxChoice::Create(parent, id, pos, size, choices, style, validator);
    
    // add items to the control
    wxString strqry;
    strqry = wxT("SELECT id || ' ' || name FROM customer ORDER BY name");
    
    // get rows from table
    PGresult *res;
    
    res = PQexec( wxGetApp().conn, strqry.char_str() );
    if (PQresultStatus(res) != PGRES_TUPLES_OK)
    {
        //wxMessageBox(_("Unable to get record from table"));
        PQclear(res);
        return false;
    }
    
    // next, add the rows to choice control
    wxString CustomerName;
    int counter = 0;
    while ( counter <= PQntuples(res)-1 )
    {
        CustomerName.Empty();
        CustomerName = wxT(PQgetvalue(res, counter, 0));
        Append(CustomerName);
        counter++;
    };
    
    // clear the result and others to avoid memory leak
    PQclear(res);
    strqry.Clear();
    
    return true;
}


/*!
 * choiceCustomer destructor
 */

choiceCustomer::~choiceCustomer()
{
}


wxString choiceCustomer::GetSelected()
{
    wxString Selected;
    Selected = GetStringSelection();
    Selected = Selected.Left(5);
    
    return Selected;
}


bool choiceCustomer::SetSelected(wxString& ID)
{
    // add items to the control
    wxString strqry;
    strqry = wxT("SELECT id || ' ' || name FROM customer WHERE id = ") + ID;
    
    // get rows from table
    PGresult *res;
    
    res = PQexec( wxGetApp().conn, strqry.char_str() );
    if (PQresultStatus(res) != PGRES_TUPLES_OK)
    {
        //wxMessageBox(_("Unable to get record from table"));
        PQclear(res);
        return false;
    };
    
    // next, add the rows to choice control
    if ( PQntuples(res) >= 1 )
    {
        wxString Customer;
        Customer = wxT(PQgetvalue(res, 0, 0));
        SetStringSelection(Customer);
    };
    
    // clear the result and others to avoid memory leak
    PQclear(res);
    strqry.Clear();
    
    return true;
}
